﻿var $AreaErrorMsg;
var $area_pesquisar;
var $area_cadastro;

$(function(){

	$area_pesquisar = $("#form_area_pesquisar");
	$area_cadastro	= $("#area_form");

	getAreas(1);
	getStatus();

	$(".keypress").on("keypress",function(e){
		var code = e.keyCode || e.which;
		if(code == 13) { //Enter keycode
			getAreas(1);
		}
	});

	$inputNome = $("input#nome");

	var $AreaErrorMsg =	"<br>Erro ao inserir!";

	$area_cadastro.validate({
		rules:{
            nome:{
                // campoNome será obrigatorio (required) e terá tamanho minimo (minLength)
                required: true, minlength: 2, maxlength : 40
            }
        },
        // Define as mensagens de erro para cada regra
        messages:{
            nome:{
                required	: "<br>Informe o nome",
                minlength   : "<br>Digite no m&iacute;nimo, 2 (sete) caracteres",
                maxlength   : "<br>Digite no m&aacute;ximo, 40 (sete) caracteres"
            },
            cmb_cod_status : {

            }
        },
        errorPlacement: function( error, element ) {
			error.html() == $AreaErrorMsg ? $(".area-credentials").after(error) : error.insertAfter(element);
		},
		submitHandler: function(form) {
			bi.LightboxExibe({lightbox: "load"});
			$.ajax({
				type : 'POST', 
				url  : 'ajax/insere.asp?acao=setArea',
				data : {
				nome 	    		    : $inputNome.attr("value"), 
				cod_status_area		    : $("select#cmb_cod_status").attr("value"),
				cod_area			    : $("input#cod_area").attr("value")
				},
				success : function(data){
					bi.LightboxEsconde({lightbox: "lgt_novo"});
					bi.LightboxEsconde({lightbox: "load"});
					bi.Alertas({tipo : 'sucesso', texto : 'Dados cadastrado com sucesso!',titulo : 'Aviso'})
					$("#nome").attr("value",'');
					getAreas($("input#pagina").attr("value"));						
				}
			});
		}
	});

	$area_pesquisar.validate({
        submitHandler: function(form){
	        getAreas(1);
        }
	});

});


function gravar(){
	$area_cadastro.submit();
	return false;
}

function pesquisar(){
	$area_pesquisar.submit();
	return false;
}

function abreLgh(editar){
    if(editar == 0){
        $("#nome").attr("value","");
        $("#cod_area").attr("value","0");
        $("#cmb_cod_status").attr("value","");
        $("#edit_status").css("display","");
        $("#view_status").css("display","none");
        $('#nome').attr('readonly', false);                 
        $(".btn_salvar").css("display","");
    }
    bi.LightboxExibe({lightbox: "lgt_novo"});
    return false;
}


function getEditarArea($cod_area){
	bi.LightboxExibe({lightbox: "load"});
	$.ajax({
		type : 'POST',
		dataType: "text",
		url : 'ajax/area_pesquisar.asp?acao=getEditarArea',
		data : {cod_area : $cod_area},
		success : function(data){
			if (data){
				var inf = eval("(" + data + ")");
				$("#cod_area").attr("value",inf.data.cod_area);
				if(inf.data.cod_usuario_tipo == 1){
					$('#inf_cadastro_status').html(inf.data.desc_status);
					$("#inf_cadastro_area").html(inf.data.nome);
					$("#edit_status").css("display","none");
					$("#edit_area").css("display","none");
					$("#view_status").css("display","");
					$("#view_area").css("display","");
					$(".btn_salvar").css("display","none");
				}else{
					$("#nome").attr("value",inf.data.nome);
					$("#cmb_cod_status").attr("value",inf.data.cod_status);
					$("#edit_status").css("display","");
					$("#edit_area").css("display","");
					$("#view_status").css("display","none");
					$("#view_area").css("display","none");
					$(".btn_salvar").css("display","");
				}
				bi.LightboxExibe({lightbox: "lgt_novo"});
				bi.LightboxEsconde({lightbox: "load"});
			}
		},
		error : function (error){
			bi.LightboxEsconde();
			alert(error.responseText);
		} 
	});
	return false;
}

function getStatus(){
	$.ajax({
		type : 'POST',
		url  : 'ajax/area_pesquisar.asp',
		data : {acao : 'getStatus'},
		success : function(data){
			$("#cmb_cod_status").html(data);
		}
	});
}


function getAreas($pg_sel){
	bi.LightboxExibe({lightbox: "load"});
	$.ajax({
		type : 'POST',
		dataType : 'text',
		url  : 'ajax/area_pesquisar.asp',
		data : {
			nome 	: $("#filtro_area").attr("value"),
			pg_sel 	: $pg_sel,
			acao	: "getAreas"
		},
		success : function(data){
			$("#retorno_areas").html(data);
			bi.LightboxEsconde({lightbox: "load"});
		},
		error : function (error){
			bi.LightboxEsconde({lightbox: "load"});
			alert(error.responseText);
		}
	});
	return false;
}
